package app.project.directionlensproject.util;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import app.project.directionlensproject.model.Place;
import app.project.directionlensproject.util.GoogleMapUtil;

public class GoogleMapUtil {

	public static GoogleMapUtil instance;

	public static GoogleMapUtil getInstance() {
		if (instance == null) {
			instance = new GoogleMapUtil();
		}

		return instance;
	}
	
	/**get place near by my place from google map
	 * @param myPlace
	 * @return
	 */
	public Place getNearByPlace(Place myPlace,int radius,int type){
		String url="https://maps.googleapis.com/maps/api/place/radarsearch/json?location="+myPlace.locationx+","+myPlace.locationy+"&radius="+radius+"&types=food|cafe";
		return null;
	}
	
	public ArrayList<Place> initGooglePlace(String jsonReponse) throws Exception{
		ArrayList<Place> result = new ArrayList<Place>();
		JSONObject jsonObject = new JSONObject(jsonReponse);
		JSONArray resultsArray = jsonObject.getJSONArray("results");
		int length = resultsArray.length();
		for(int i = 0; i<length; i++){
			JSONObject tmpObject = resultsArray.getJSONObject(i);
			JSONObject geometryObject = tmpObject.getJSONObject("geometry");
			JSONObject locationObject = geometryObject.getJSONObject("location");
			Place p = new Place();
			p.locationx = (float) locationObject.getDouble("lat");
			p.locationy = (float) locationObject.getDouble("lng");
			p.placename = tmpObject.getString("name");
			p.description = tmpObject.getString("formatted_address");
			result.add(p);
		}
		return result;
	}
	
}
